package com.dmlll.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmlll.VO.AreaVO;
import com.dmlll.common.DataGridView;
import com.dmlll.common.ResultObj;
import com.dmlll.common.TreeType;
import com.dmlll.domain.Area;
import com.dmlll.service.AreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@RestController
@RequestMapping("area")
public class AreaController {
    @Resource
    private AreaService areaService;

    @RequestMapping("loadArea")
    public DataGridView loadArea(AreaVO area) {
        IPage<Area> page = new Page<>(area.getPage(), area.getLimit());
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(area.getType() != null&&!area.getType().equals("3"), "type", area.getType());
        queryWrapper.like(StringUtils.isNotBlank(area.getAreaName()), "area_name", area.getAreaName());
        IPage<Area> areaIPage = areaService.page(page, queryWrapper);
        return new DataGridView(areaIPage.getTotal(), areaIPage.getRecords());
    }
    @RequestMapping("getAllArea")
    public List<Area> getAllArea(){
        return areaService.list();
    }
    @RequestMapping("getPidName")
    public String getPidName(Integer id) {
        return areaService.getById(id).getAreaName();
    }

    @RequestMapping("add")
    public ResultObj add(AreaVO areaVO) {
        try {
            if(areaVO.getPid()==null)
                areaVO.setPid(0);
            areaService.save(areaVO);
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }
    @RequestMapping("update")
    public ResultObj update(AreaVO areaVO) {
        try {
            areaService.updateById(areaVO);
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;

        }
        return ResultObj.UPDATE_SUCCESS;
    }
    @RequestMapping("delete")
    public ResultObj delete(AreaVO areaVO){
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",areaVO.getId());
        List<Area> list = areaService.list(queryWrapper);
        if(list.size()>0){
            return ResultObj.DELETE_ERROR;
        }
        areaService.removeById(areaVO.getId());
        return ResultObj.DELETE_SUCCESS;

    }

    @RequestMapping("loadTree")
    public DataGridView loadTree() {
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("type", 1);//查询出市级的区域
        List<Area> areaList = areaService.list(queryWrapper);
        List<TreeType> treeTypeList = new ArrayList<>();
        areaList.stream().forEach(area -> {
            TreeType treeType = new TreeType(area.getId(), area.getAreaName(), area.getPid(),false);
            treeTypeList.add(treeType);//所有的父级
        });
        return new DataGridView(treeTypeList);
    }
}

